package com.example.spider.proxy.core.tester;

import java.net.Proxy;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public enum ProxyTypeEnum {

    /**
     * local no proxy
     */
    DIRECT(Proxy.Type.DIRECT),

    HTTP(Proxy.Type.HTTP),
    HTTPS(Proxy.Type.HTTP),
    SOCKS4(Proxy.Type.SOCKS),
    SOCKS5(Proxy.Type.SOCKS),

    UNKNOWN(Proxy.Type.HTTP),
    ;

    private static final Map<String, ProxyTypeEnum> nameMap = nameMap();
    private final Proxy.Type type;

    ProxyTypeEnum(Proxy.Type type) {
        this.type = type;
    }

    private static Map<String, ProxyTypeEnum> nameMap() {
        Map<String, ProxyTypeEnum> map = Arrays.stream(values()).collect(Collectors.toMap(Enum::name, t -> t));
        map.put("SOCKS", SOCKS4);
        return map;
    }

    public static ProxyTypeEnum of(String type) {
        return Optional.ofNullable(type)
                .map(String::trim)
                .map(String::toUpperCase)
                .map(nameMap::get)
                .orElse(UNKNOWN);
    }

    public Proxy.Type getType() {
        return type;
    }
}
